var WxParse = require('../../wxParse/wxParse.js');
var request = require('../../utils/request.js');
var app = getApp();
Page({
  data: {},
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数

  },
  onShow: function () {
    var that = this;
    that.getToken();
    console.log(getCurrentPages())
    //你可以通过getCurrentPages()获取页面page对象，执行非当前页js里的方法
  },
  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        var tempToken = res.data;
        request.getUser(
          { "session_id": tempToken },
          (res) => {
            console.log(res);
            if (res.data != "") {

              if (res.data.phone.length > 0) {
                that.setData({
                  phoneTemp: res.data.phone.substr(0, 3) + '****' + res.data.phone.substr(7),
                  list: res.data
                })
              } else {
                that.setData({
                  list: res.data,
                })
              }
            }
            else {
              that.onShow();
            }

          }
        )
      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },

  //跳转到钱包
  goWallet: function () {
    wx.navigateTo({
      url: '../my/my-wallet/my-wallet',
    })
  },
  //跳转到借阅记录
  goLoanRecord: function () {
    wx.navigateTo({
      url: '../my/my-loanRecord/my-loanRecord?curId=1',
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    var postData = {
      "session_id": that.data.token,
      "iv": e.detail.iv,
      'encryptedData': e.detail.encryptedData
    }
    console.log(postData)
    request.bindPhone(postData,
      (res) => {
        console.log(res)
        if (res.data == 1 ||res.data == '1' || res.data == 1002) {//1绑定成功 1003绑定失败 1002已绑定过 1001请求失败
          request.getUser(
            {
              "session_id": that.data.token
            },
            (res) => {
              console.log(res);
              if (res.data != "") {
                console.log("手机号")
                that.data.list.phone = res.data.phone;
                that.setData({
                  list: that.data.list,
                  phoneTemp: res.data.phone.substr(0, 3) + '****' + res.data.phone.substr(7)
                })
                that.getToken();
              }
              else {
                that.getToken();
              }
            }
          )
        } else if (res.data == 1003 || res.data == 1001) {
          wx.showToast({
            title: '获取手机号失败',
            image: "../../images/xx.png"
          })
        }
      })
  }

})





